/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.AksesMatrixDAO;
import com.ivanbiz.dao.GroupsDAO;
import com.ivanbiz.dao.impl.AksesMatrixDAOImpl;
import com.ivanbiz.dao.impl.GroupsDAOImpl;
import com.ivanbiz.model.AksesMatrix;
import com.ivanbiz.model.Groups;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author IW20149X
 */
public class GroupActionService extends SelectorComposer<Component> {

    GroupsDAO groupsDAO = new GroupsDAOImpl();
    @Wire
    Window win;
    @Wire
    Textbox nama;
    @Wire
    Listbox listMenu;
    Groups groups;
    List<AksesMatrix> listAksesMatrixView;
    List<AksesMatrix> listAksesMatrix;
    AksesMatrixDAO aksesMatrixDAO;
//    List<Checkbox> selectedCheckboxs;

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);
//        selectedCheckboxs = new ArrayList<Checkbox>();

        listAksesMatrixView = new ArrayList<AksesMatrix>();
        aksesMatrixDAO = new AksesMatrixDAOImpl();
        groups = (Groups) Executions.getCurrent().getArg().get("group");
        if (groups == null) {
            listAksesMatrix = new ArrayList<AksesMatrix>();
        } else {
            listAksesMatrix = aksesMatrixDAO.getDataByEquals(AksesMatrix.class, "groups.id", groups.getId());
        }
        renderTableAkses();
    }

    @Listen("onClick=#simpan;")
    public void simpan() {
        if (groups == null) {
            groups = new Groups();
        }
        groups.setNama(nama.getValue());
        validateGroup(groups);
    }

    private void validateGroup(Groups groups) {
        if (groups.getNama().trim().isEmpty()) {
            Messagebox.show("Nama tidak boleh kosong");
        } else {
            try {
                if (win.getTitle().equals("Ubah Group")) {
                    List<AksesMatrix> list = aksesMatrixDAO.getDataByEquals(AksesMatrix.class, "groups.id", groups.getId());
                    for (AksesMatrix aksesMatrix : list) {
                        aksesMatrixDAO.delete(aksesMatrix);
                    }
                }
                List<Listitem> item = listMenu.getItems();
                List<AksesMatrix> listAksesMatrixs = new ArrayList<AksesMatrix>();
                for (Listitem listitem : item) {
                    List<Component> com = listitem.getChildren();
                    for (Component compCell : com) {
                        Listcell list = (Listcell) compCell;
                        if (list.getFirstChild() instanceof Checkbox) {
                            Checkbox checkbox = (Checkbox) list.getFirstChild();
                            if (checkbox.isChecked() == true) {
                                AksesMatrix aksesMatrixs = new AksesMatrix();
                                aksesMatrixs.setGroups(groups);
                                aksesMatrixs.setNama((String) checkbox.getValue());
                                listAksesMatrixs.add(aksesMatrixs);                                
                            }
                        }
                    }
                }
                groupsDAO.saveOrUpdate(groups, listAksesMatrixs);
                win.detach();
            } catch (Exception ex) {
                Logger.getLogger(GroupActionService.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void renderTableAkses() {
        for (MenuAksesConstant menuAksesConstant : MenuAksesConstant.values()) {
            AksesMatrix aksesMatrix = new AksesMatrix();
            aksesMatrix.setNama(menuAksesConstant.toString());
            aksesMatrix.setMenuChecked(MenuAksesConstant.validate(menuAksesConstant, listAksesMatrix));
            listAksesMatrixView.add(aksesMatrix);
        }
        updateTableAkses();
    }

    private void updateTableAkses() {
        int x = 0;
        for (AksesMatrix aksesMatrix : listAksesMatrixView) {
            Listitem item = new Listitem();
            item.setValue(aksesMatrix);
            Checkbox checkbox = new Checkbox();
            checkbox.setChecked(aksesMatrix.isMenuChecked());
            checkbox.setValue(aksesMatrix.getNama());
            Listcell cellOperasi = new Listcell();
            cellOperasi.appendChild(checkbox);
            item.appendChild(cellOperasi);
            item.appendChild(new Listcell(aksesMatrix.getNama()));
            listMenu.appendChild(item);
            ++x;
        }
    }
}
